<template>
  <div id="stu">
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="编号" width="100">
        <template slot-scope="scope">
          <span>{{ scope.$index * 1 + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="药物id" width="200">
        <template slot-scope="scope">
          <span>{{ scope.row.mediNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="药物名" width="300">
        <template slot-scope="scope">
          <span>{{ scope.row.medicineTypes.mediName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="药物用量" width="200">
        <template slot-scope="scope">
          <span>{{ scope.row.quantity }}</span>
        </template>
      </el-table-column>
      <el-table-column label="药物用法" width="600">
        <template slot-scope="scope">
          <span>{{ scope.row.medicineTypes.specification }}</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="rewriter(scope.$index, scope.row)"
            type="text"
            size="small"
          >
            修改
          </el-button>
          <el-button
            @click.native.prevent="delRow(scope.row.id)"
            type="text"
            size="small"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-for="(d, index) in counter" class="getchufangall" :key="index">
      <Addchufang></Addchufang>
    </div>
    <el-button type="success" @click="Addchufang()" round>添加药物</el-button>
    <el-button type="success" @click="subnewlist()" round>提交</el-button>
    <ADD
      @close="closeshow"
      :fromdata="from"
      :sublist="sublist"
      :theID="theID"
      :kind="kind"
      :dialogFormVisible="isshow"
    ></ADD>
  </div>
</template>

<script>
import ADD from "@/components/add&rewriter";
import Addchufang from "@/components/addchufang";
import { mapActions, mapMutations, mapGetters } from "vuex";
export default {
  data() {
    return {
      tableData: [],
      // 添加药物
      counter: [{}],
      isshow: false,
      //弹出框的值
      kind: null,
      theID: null,
      from: {},
      searchkey: "",
      searchkind: "",
      rewritebefor: {},
      rewrite: {},
      sublist: null
    };
  },
  components: {
    Addchufang,
    ADD
  },
  mounted() {
    this.countpage(this.$route.query.billno);
  },
  methods: {
    ...mapActions(["tongyong"]),
    async countpage(nowbillno) {
      const res = await this["tongyong"]({
        name: "GET_SHOWCHUFAN",
        data: {
          billNo: nowbillno
        }
      });
      if (res.data.msg != "不存在此药帐单") {
        this.rewritebefor = res.data.data;
        // this.tableData = res.data.data.drugMes; 
        let druglist=[]
        for(var item of  res.data.data.drugMes){

          if(item.prescription){
            druglist.push(item)
          }
        } 
        console.log('123123',druglist);
        res.data.data.drugMes=druglist
         this.tableData = res.data.data.drugMes; 
        // console.log('all',);
      } else {
        this.$message({
          message: "不存在此药帐单",
          type: "warning"
        });
    
        const res = await this["tongyong"]({
          name: "GET_DELCHUFANG",
          data: { billNo: this.$route.query.billno }
        });
        if (res.status == 200) {
          this.$router.push({ name: "countdata" });
        }

      }
    },
    async delRow(id) {
      const res = await this["tongyong"]({
        name: "GET_DELYAO",
        data: {
          id: id
        }
      });
      if (res.status == 200) {
        this.$message({
          showClose: true,
          message: "删除成功",
          type: "success"
        });
        
      }
      this.countpage(this.$route.query.billno);
    },
    rewriter(a, redata) {
      // 初始化
      this.sublist = redata.prescription;
      this.from = {};
      this.kind = "rewriter";
      this.from = { 剂量: redata.prescription.quantity };
      (this.theID = redata.id), this.openshow();
    },
    Addchufang() {
      this.counter.push({});
    },
    async subnewlist() {
      const res = await this["tongyong"]({
        name: "GET_UPNEWYAO",
        data: this.getall()
      });
      if(res.status=='200'){
        this.$message({
            message: "添加成功",
            type: "warning"
          });
        this.countpage(this.$route.query.billno);
        this.counter=[]
          this.Addchufang()
      }else{
           this.$message({
            message: "添加失败",
            type: "warning"
          });
          this.counter=[]
          this.Addchufang()
      }
      //  this.countpage(this.$route.query.billno);
    },
    getall() {
      var data = [];
      for (let i = 0; i < $(".getchufangall ").length; i++) {
        if (
          !$(".everoneid>input")[i].value ||
          !$(".everoneneed>input")[i].value
        ) {
          this.$message({
            message: "提交前请确定你的处方单是否有空白",
            type: "warning"
          });
        } else {
          data.push({
            billNo: this.$route.query.billno,
            mediNo: $(".everoneid>input")[i].value,
            quantity: $(".everoneneed>input")[i].value
          });
        }
      }
      return data;
    },

    // 对话框开关
    closeshow(data) {
      this.isshow = data;
       this.countpage(this.$route.query.billno);
    },
    openshow() {
      this.isshow = true;
    }
  }
};
</script>

<style>
#stu {
  min-width: 450px;
}
.el-pagination {
  float: right;
}
.pagination {
  margin-top: 10px;
  float: right;
  width: 100%;
  background: white;
}
.search {
  display: flex;
  justify-content: center;
  margin-bottom: 10px;
}
.el-button + .el-button {
  margin: 0;
}
</style>
